home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / comm / net / tf02.lha / TinyFugue / tf-lib / hanoi.tf < prev    next >
Text File  |  1995-08-12  |  728b  |  28 lines

  1. ;;;; Towers of Hanoi
  2. ; syntax:  /hanoi <n>
  3. ; Solves the classic Towers of Hanoi problem for <n> disks.
  4. ; Not very useful except as an example of recursion, and demonstrating
  5. ; tf's similarity to a shell scripting language.
  6.  
  7. /def hanoi = /do_hanoi %{1-0} 1 3 2
  8.  
  9. /def do_hanoi = \
  10.     /if /test {1} > 0%; /then \
  11.         /do_hanoi $[{1} - 1] %2 %4 %3%;\
  12.         :moves a disk from post %2 to %3%;\
  13.         /do_hanoi $[{1} - 1] %4 %3 %2%;\
  14.     /endif
  15.  
  16. ;;; For comparison, here's the same algorithm in Bash:
  17.  
  18. ; hanoi() { do_hanoi ${1-0} 1 3 2; }
  19. ;
  20. ; do_hanoi() {
  21. ;     if test $1 -gt 0; then
  22. ;         do_hanoi $[$1 - 1] $2 $4 $3
  23. ;         echo You move a disk from post $2 to $3
  24. ;         do_hanoi $[$1 - 1] $4 $3 $2
  25. ;     fi
  26. ; }
  27.  
  28.